import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import * as dto from './DTO';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/sales/api/v1';

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询列表'
});
const submitDesc = formatMessage({
    id: 'api.submitDesc',
    defaultMessage: '提交'
});
const getAbandonDesc = formatMessage({
    id: 'api.getAbandon',
    defaultMessage: '作废'
});
const exportDataDesc = formatMessage({
    id: 'api.exportDataDesc',
    defaultMessage: '导出'
});
const getMarketDepartmentDesc = formatMessage({
    id: 'api.message.getMarketDepartmentDesc',
    defaultMessage: '查询大区'
});
const getManufacturerListDesc = formatMessage({
    id: 'api.message.getManufacturerListDesc',
    defaultMessage: '查询生产工厂'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/brands`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));
// 查询列表
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/salePlans?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 提交
export const submitData = data => request(`${baseUrl}/salePlans/submit`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitDesc));
// 作废
export const abandonData = data => request(`${baseUrl}/salePlans/abandon`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAbandonDesc));
//导入模板
export const importTemp = () => request(`${baseUrl}/salePlans/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);
// 导出
export const exportData = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/salePlans/export?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, exportDataDesc));
};
// 查询大区
export const getMarketDepartment = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};
// 查询大区
export const getMarketDepartmentSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};
// 查询生产工厂
export const getManufacturer = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/manucturers/forSalePlanOrder?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getManufacturerListDesc));
};
export const getManufacturerSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/manucturers/forSalePlanOrder/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getManufacturerListDesc));
};
